local hx__donghuangzhong = fk.CreateSkill{
  name = "hx__donghuangzhong",

  tags = {Skill.Compulsory},
}

hx__donghuangzhong:addEffect("active",{
  name = "hx__donghuangzhong",
  can_use = function (self, player)
    return player:usedSkillTimes(hx__donghuangzhong.name) < 2
  end,
  interaction = function (self, player)
    local all_choices = {"hx__donghuangzhong__self","hx__donghuangzhong__other"}
    local choices = {}
    if #player:getTableMark("hx__donghuangzhong_selfrecord") ~= 0 then
      table.insert(choices,"hx__donghuangzhong__self")
    end
    if #player:getTableMark("hx__donghuangzhong_ohterrecord") > 0 then
      table.insert(choices,"hx__donghuangzhong__other")
    end
    if #choices == 0 then
        all_choices = {"hx__donghuangzhong__norecord"}
        choices = {"hx__donghuangzhong__norecord"}
    end
    return UI.ComboBox{choices = choices, detailed = true, all_choices = all_choices,}
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  min_target_num = 0,
  max_target_num = 1,
  target_filter = function (self, player, to_select, selected, selected_cards)
    if not self.interaction.data or self.interaction.data == "hx__donghuangzhong__norecord" then return end
    if self.interaction.data == "hx__donghuangzhong__self" then
      return false
    elseif self.interaction.data == "hx__donghuangzhong__other" then
      local records = player:getTableMark("hx__donghuangzhong_ohterrecord")
      local targets = {}
      for _, v in ipairs(records) do
        table.insert(targets,Fk:currentRoom():getPlayerById(v[1]))
      end
      return #selected == 0 and table.contains(targets,to_select)
    end
  end,
  on_use =function (self, room, effect)
    local player = effect.from
    if self.interaction.data == "hx__donghuangzhong__self" then
      local record = player:getTableMark("hx__donghuangzhong_selfrecord")
      player:throwAllMarks()
      for _, v in ipairs(record) do
        if v[1] == "hx__donghuangzhong_hp" then
          room:changeHp(player,v[2] - player.hp)
        else
          room:setPlayerMark(player,v[1],v[2])
        end
      end
      player:drawCards(3,hx__donghuangzhong.name)
    else
      if #effect.tos == 0 then return end
      local target = effect.tos[1]
      local record = player:getTableMark("hx__donghuangzhong_ohterrecord")
      for _, v in ipairs(record) do
        if v[1] == target.id then
          room:changeMaxHp(target,v[3] - target.maxHp)
          room:changeHp(target,v[2] - target.hp)
          if target:getHandcardNum() > v[4] then
            local num = target:getHandcardNum() - v[4]
            room:askToDiscard(target,{ min_num = num , max_num = num , skill_name = self.name, cancelable = false, include_equip = false, pattern = "." , prompt = "#hx__donghuangzhong__dicard:::"..num})
          else
            local num = v[4] - target:getHandcardNum()
            target:drawCards(num,hx__donghuangzhong.name)
          end
          room:changeMaxHp(target,-1)
          break
        end
      end
    end
  end,
})

hx__donghuangzhong:addEffect(fk.TurnEnd,{
  name = "#hx__donghuangzhong__record",
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(hx__donghuangzhong.name)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if target == player  then
      room:setPlayerMark(player,"hx__donghuangzhong_selfrecord",0)
      local mark = player:getMarkNames()
      for _, v in ipairs(mark) do
        local real = player:getMark(v)
        if real then
          room:addTableMark(player,"hx__donghuangzhong_selfrecord",{v,real})
        end
      end
      room:addTableMark(player,"hx__donghuangzhong_selfrecord",{"hx__donghuangzhong_hp",player.hp})
    end
    local records = player:getTableMark("hx__donghuangzhong_ohterrecord")
    local currentrecord = table.find(records,function (sub) return sub[1] == target.id end)
      if currentrecord then
        room:removeTableMark(player,"hx__donghuangzhong_selfrecord",currentrecord)
        room:addTableMark(player,"hx__donghuangzhong_ohterrecord",{target.id,target.hp,target.maxHp,target:getHandcardNum()})
      else
        room:addTableMark(player,"hx__donghuangzhong_ohterrecord",{target.id,target.hp,target.maxHp,target:getHandcardNum()})  
      end
  end,
})


return hx__donghuangzhong